<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="evaluation-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">测评需求编号</label>
        <div class="layui-input-block">
            <input type="text" name="demandNo" lay-verify="required" value="" class="layui-input" readonly>
            <input type="text" name="id" value="" hidden>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">测评活动名称</label>
        <div class="layui-input-inline">
            <input type="text" name="subject" lay-verify="required" placeholder="请填写测评活动名称" value="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：测评活动名称，将在发起测评时使用，请慎重填写，尽量确保不重复。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">A级测评报酬(元)</label>
        <div class="layui-input-inline">
            <input type="text" name="levelAReward" lay-verify="required" placeholder="请填写A级测评报酬" value="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：若测评报告被评定为A级时测评报酬的总金额。评级结束后，自动补发A级测评报酬-C级测评报酬的额度。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">A级名额(人)</label>
        <div class="layui-input-inline">
            <input type="text" name="levelAQuota" lay-verify="required" placeholder="请填写A级名额" value="" class="layui-input" oninput="calculatedQuota()">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：整个测评过程中预计最多被评定为A级的人数（包含）。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">A级评级标准</label>
        <div class="layui-input-block">
            <textarea name="levelACriterion" class="layui-textarea" placeholder="请填写A级评级标准"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">B级测评报酬(元)</label>
        <div class="layui-input-inline">
            <input type="text" name="levelBReward" lay-verify="required" placeholder="请填写B级测评报酬" value="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：若测评报告被评定为B级时测评报酬的总金额。评级结束后，自动补发B级测评报酬-C级测评报酬的额度。</div>
    </div>
    <div class="layui-form-item ">
        <label class="layui-form-label required">B级名额(人)</label>
        <div class="layui-input-inline">
            <input type="text" name="levelBQuota" lay-verify="required" placeholder="请填写B级名额" value="" class="layui-input" oninput="calculatedQuota()">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：整个测评过程中预计最多被评定为B级的人数（包含）。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">B级评级标准</label>
        <div class="layui-input-block">
            <textarea name="levelBCriterion" class="layui-textarea" placeholder="请填写B级评级标准"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">C级测评报酬(元)</label>
        <div class="layui-input-inline">
            <input type="text" name="levelCReward" lay-verify="required" placeholder="请填写C级测评报酬" value="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：参与测评并提交测评报告，若测评报告符合条件被验收后自动给予的测评报酬。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">C级名额(人)</label>
        <div class="layui-input-inline">
            <input type="text" name="levelCQuota" lay-verify="required" placeholder="请填写C级名额" value="" class="layui-input" oninput="calculatedQuota()">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：整个测评过程中预计最多被评定为C级的人数（包含或超出）。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">C级评级标准</label>
        <div class="layui-input-block">
            <textarea name="levelCCriterion" class="layui-textarea" placeholder="请填写C级评级标准"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">测评总费用(元)</label>
        <div class="layui-input-inline">
            <input type="text" name="purchasePrice" lay-verify="required" placeholder="请填写测评总费用" value="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：测评总费用=A级测评报酬(元)*A级名额(人)+B级测评报酬(元)*B级名额(人)+C级测评报酬(元)*C级名额(人)，不可编辑。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">测评总人数(人)</label>
        <div class="layui-input-inline">
            <input type="text" name="joinQuota" lay-verify="required" placeholder="请填写测评总费用" value="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">说明：测评人数为自动计算=A级名额(人)+B级名额(人)+C级名额(人)，不可编辑。</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">测评要求设置</label>
        <div class="layui-input-block">
            <div id="editor1" style="margin: 50px 0 50px 0"></div>
            <input type="text" name="remarks" value="" hidden>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">验收要求设置</label>
        <div class="layui-input-block">
            <div id="editor2" style="margin: 50px 0 50px 0"></div>
            <input type="text" name="requirements" value="" hidden>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    let editor1;
    let editor2;

    window.onload=function(){
        let id = GetQueryString("id");
        if(isNotNull(id)){
            init(id);
        }else{
            let iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }

    /**
     * 初始化数据
     */
    function init(id) {
        $.ajax({
            type:"get",
            url:'/evaluation/'+id,
            dataType:'json',
            contentType: 'application/x-www-form-urlencoded',
            success:function(req){
                layui.use(['form'], function () {
                    let form = layui.form
                    form.val('evaluation-form',{
                        "id": req.data.id,
                        "demandNo": req.data.demandNo,
                        "subject": req.data.subject,
                        "levelAReward": req.data.levelAReward,
                        "levelAQuota": req.data.levelAQuota,
                        "levelACriterion": req.data.levelACriterion,
                        "levelBReward": req.data.levelBReward,
                        "levelBQuota": req.data.levelBQuota,
                        "levelBCriterion": req.data.levelBCriterion,
                        "levelCReward": req.data.levelCReward,
                        "levelCQuota": req.data.levelCQuota,
                        "levelCCriterion": req.data.levelCCriterion,
                        "purchasePrice": req.data.purchasePrice,
                        "joinQuota": req.data.joinQuota,
                        "remarks": req.data.remarks,
                        "requirements": req.data.requirements
                    });
                    if(isNotNull(req.data.remarks)){
                        initEditor1(req.data.remarks);
                    }
                    if(isNotNull(req.data.requirements)){
                        initEditor2(req.data.requirements);
                    }
                })
            },
            error:function(e){
                layer.msg("数据有误");
            }
        })
    }

    layui.use(['form','layarea'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            $ = layui.$;

        layarea.render({
            elem: '#area-picker',
            data: {},
            change: function (res) {
                //选择结果
                let area = res.province + "-" + res.city;
                $('input[name="area"]').val(area);
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert('确定提交修改吗？', {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...",{
                    icon:16,
                    time:-1
                })
                $.ajax({
                    type:"post",
                    url:'/evaluation/edit',
                    data:JSON.stringify(data.field),
                    dataType:'json',
                    contentType: 'application/json; charset=UTF-8',
                    success:function(req){
                        layer.msg("修改成功");
                        layer.close(index);
                        // 关闭弹出层
                        setTimeout(function () {
                            layer.close(myMsg);//手动关闭
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                    },
                    error:function(e){
                        layer.msg("修改失败");
                        layer.close(myMsg);//手动关闭
                    }
                })
            });

            return false;
        });

    });

    //富文本编辑器1
    layui.use(['layer','wangEditor'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            wangEditor = layui.wangEditor;
        editor1 = new wangEditor('#editor1');
        editor1.customConfig.uploadImgServer = "../api/upload.json";
        editor1.customConfig.uploadFileName = 'image';
        editor1.customConfig.pasteFilterStyle = false;
        editor1.customConfig.uploadImgMaxLength = 5;
        editor1.customConfig.uploadImgHooks = {
            // 上传超时
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
            customInsert: function (insertImg, result, editor) {
                console.log(result);
                if (result.code == 1) {
                    var url = result.data.url;
                    url.forEach(function (e) {
                        insertImg(e);
                    })
                } else {
                    layer.msg(result.msg);
                }
            }
        };
        editor1.customConfig.customAlert = function (info) {
            layer.msg(info);
        };
        editor1.customConfig.onchange = function (html) {
            $('input[name="remarks"]').val(html);
        };
        editor1.create();
    });

    //富文本编辑器2
    layui.use(['layer','wangEditor'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            wangEditor = layui.wangEditor;
        editor2 = new wangEditor('#editor2');
        editor2.customConfig.uploadImgServer = "../api/upload.json";
        editor2.customConfig.uploadFileName = 'image';
        editor2.customConfig.pasteFilterStyle = false;
        editor2.customConfig.uploadImgMaxLength = 5;
        editor2.customConfig.uploadImgHooks = {
            // 上传超时
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
            customInsert: function (insertImg, result, editor) {
                console.log(result);
                if (result.code == 1) {
                    var url = result.data.url;
                    url.forEach(function (e) {
                        insertImg(e);
                    })
                } else {
                    layer.msg(result.msg);
                }
            }
        };
        editor2.customConfig.customAlert = function (info) {
            layer.msg(info);
        };
        editor2.customConfig.onchange = function (html) {
            $('input[name="requirements"]').val(html);
        };
        editor2.create();
    });

    /**
     * 初始化富文本编辑器数据1
     */
    function initEditor1(html) {
        editor1.txt.html(html);
    }
    /**
     * 初始化富文本编辑器数据2
     */
    function initEditor2(html) {
        editor2.txt.html(html);
    }
    /**
     * 计算名额
     */
    function calculatedQuota(){
        let temp = 0;

        let LevelAQuota = $('input[name="levelAQuota"]').val();// A级名额(人)
        let LevelBQuota = $('input[name="levelBQuota"]').val();// B级名额(人)
        let LevelCQuota = $('input[name="levelCQuota"]').val();// C级名额(人)

        if(isNotNull(LevelAQuota)){
            temp += parseInt(LevelAQuota);
        }

        if(isNotNull(LevelBQuota)){
            temp += parseInt(LevelBQuota);
        }

        if(isNotNull(LevelCQuota)){
            temp += parseInt(LevelCQuota);
        }
        $('input[name="joinQuota"]').val(temp);// 测评总人数(人)

        // 计算奖励金额
        let temp_money = 0.0;

        let LevelAReward = $("input[name='levelAReward']").val();// A级测评报酬(元)
        let LevelBReward = $("input[name='levelBReward']").val();// B级测评报酬(元)
        let LevelCReward = $("input[name='levelCReward']").val();// C级测评报酬(元)

        if(isNotNull(LevelAReward)){
            if(isNotNull(LevelAQuota)){
                temp_money += parseFloat(LevelAReward) * parseFloat(LevelAQuota);
            } else {
                temp_money += parseFloat(LevelAReward);
            }
        }

        if(isNotNull(LevelBReward)){
            if(isNotNull(LevelBQuota)){
                temp_money += parseFloat(LevelBReward) * parseFloat(LevelBQuota);
            } else {
                temp_money += parseFloat(LevelBReward);
            }
        }

        if(isNotNull(LevelCReward)){
            if(isNotNull(LevelCQuota)){
                temp_money += parseFloat(LevelCReward) * parseFloat(LevelCQuota);
            } else {
                temp_money += parseFloat(LevelCReward);
            }
        }
        $('input[name="purchasePrice"]').val(temp_money.toFixed(2));// 测评总费用(元)

    }


</script>
</body>
</html>